package main

import (
	_ "routers"
	"github.com/astaxie/beego"
	"fmt"
	"github.com/astaxie/beego/orm"
	"os"
	_ "github.com/go-sql-driver/mysql"
)

func init() {
	//连接MySQL
	uicDbUser := beego.AppConfig.String("uic_mysqluser")
	uicDbPass := beego.AppConfig.String("uic_mysqlpass")
	uicDbHost := beego.AppConfig.String("uic_mysqlhost")
	uicDbPort := beego.AppConfig.String("uic_mysqlport")
	uicDbName := beego.AppConfig.String("uic_mysqldb")
	maxIdleConn, _ := beego.AppConfig.Int("db_max_idle_conn")
	maxOpenConn, _ := beego.AppConfig.Int("db_max_open_conn")
	uicDbLink := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", uicDbUser, uicDbPass, uicDbHost, uicDbPort, uicDbName) + "&loc=Asia%2FShanghai"
	//utils.Display("dbLink", dbLink)
	orm.RegisterDriver("mysql", orm.DRMySQL)
	orm.RegisterDataBase("uic", "mysql", uicDbLink, maxIdleConn, maxOpenConn)


	dbUser := beego.AppConfig.String("mysqluser")
	dbPass := beego.AppConfig.String("mysqlpass")
	dbHost := beego.AppConfig.String("mysqlhost")
	dbPort := beego.AppConfig.String("mysqlport")
	dbName := beego.AppConfig.String("mysqldb")
	dbLink := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", dbUser, dbPass, dbHost, dbPort, dbName) + "&loc=Asia%2FShanghai"
	//utils.Display("dbLink", dbLink)
	orm.RegisterDriver("mysql", orm.DRMySQL)
	orm.RegisterDataBase("default", "mysql", dbLink, maxIdleConn, maxOpenConn)

	if beego.BConfig.RunMode == "dev" {
		orm.Debug = true
	}
	//设置日志
	fn := "logs/run.log"
	if _, err := os.Stat(fn); err != nil {
		if os.IsNotExist(err) {
			os.Create(fn)
		}
	}
	beego.SetLogger("file", `{"filename":"`+fn+`"}`)
	if beego.BConfig.RunMode == "prod" {
		beego.SetLevel(beego.LevelInformational)
	}
}

func main() {
	defer func() {
		if err := recover(); err != nil {
			beego.Error("Panic error:", err)
		}
	}()
	beego.Run()
}

